package com.example.client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.aidl.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    private EditText num1, num2;
    private TextView showResult;
    private Button bind, calculate;
    private IMyAidlInterface iMyAidlInterface;
    private ServiceConnection conn = new ServiceConnection() {
        /**
         * 连接成功
         * @param componentName
         * @param iBinder
         */
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.d("=========>", "onServiceConnected...");
            Toast.makeText(MainActivity.this,"onServiceConnected",Toast.LENGTH_SHORT).show();
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
        }

        /**
         * 断开连接
         * @param componentName
         */
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d("=========>", "onServiceDisconnected...");
            Toast.makeText(MainActivity.this,"onServiceDisconnected",Toast.LENGTH_SHORT).show();
            iMyAidlInterface = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        num1 = findViewById(R.id.et_num_1);
        num2 = findViewById(R.id.et_num_2);
        showResult = findViewById(R.id.tv_result);
        bind = findViewById(R.id.btn_bind);
        calculate = findViewById(R.id.btn_calculate);

        bind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bindAIDLService();
            }
        });

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int n1 = 0;
                int n2 = 0;
                // 获取TextView中输入的数字
                n1 = Integer.parseInt(num1.getText().toString());
                n2 = Integer.parseInt(num2.getText().toString());
                try {
                    int result = iMyAidlInterface.add(n1, n2); // 计算
                    showResult.setText("" + n1 + " + " + n2 + " = " + result);
                } catch (RemoteException e) {
                    Log.i("=========>",e.toString());
                    showResult.setText("出错了");
                }
            }
        });

    }

    /**
     * 绑定远程服务
     */
    private void bindAIDLService() {
        // 获取服务
        Intent intent = new Intent();
        // 显式intent启动服务， 需要注意此处的包名和MyAIDLService类的全名
        intent.setComponent(new ComponentName("com.example.aidl", "com.example.aidl.MyAIDLService"));
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
